################################################################################
## setup
################################################################################
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("foreign", 
         "tidyverse",
         "RColorBrewer", 
         "gridExtra")
lapply(pkg, require, character.only = TRUE)

# set directory
MAIN_DIR <- "~/Dropbox/Research/Liao_Newman_Malhotra/replication/"

# set file directories
FIG_DIR <- paste(MAIN_DIR, "figures/", sep = "")
TABLE_DIR <- paste(MAIN_DIR, "tables/", sep = "")
RESULTS <- paste(MAIN_DIR, "results/", sep = "")

# create folder if doesn't exist
if (!dir.exists(FIG_DIR)) {
  print("Directory does not exist! Creating...")
  dir.create(FIG_DIR)
}
if (!dir.exists(TABLE_DIR)) {
  print("Directory does not exist! Creating...")
  dir.create(TABLE_DIR)
}
if (!dir.exists(RESULTS)) {
  print("Directory does not exist! Creating...")
  dir.create(RESULTS)
}

# load data
load(file = paste(MAIN_DIR, "nar-df.RData", sep = ""))

# exclude 2016 because data is incomplete
nar <- nar %>% filter(year < 2016)

# set parameters
#display.brewer.pal(8, "Dark2")
display.brewer.pal(9, "Set1")
brewer.pal(9, "Set1")

colors <- c(
  "#377EB8", # canada
  "#E41A1C", # china
  "#FF7F00", # india
  "#4DAF4A", # mexico
  "black" # UK
  )
            
shapes <- c(16, 17, 15, 4, 7)

axis.title.size <- 16

# plot
f.nar.sale <- ggplot(nar,
                     aes(x = year, 
                         y = sale,
                         group = factor(cty),
                         color = factor(cty),
                         shape = factor(cty))) +
  geom_point(size = 3) + 
  geom_line() + 
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_y_continuous("Sales (Billion USD)", 
                     limits = c(0, 30)) +
  scale_x_continuous("Year",
                     breaks = seq(min(nar$year), max(nar$year), by = 1),
                     limits = c(min(nar$year),
                                max(nar$year))) +
  ggtitle("Sales to Top 5 Foreign Buyers") +
  theme_bw() +
  theme(plot.title = element_text(size = axis.title.size + 2,
                                  face = "bold",
                                  hjust = 0.5,
                                  margin = margin(0, 0, 20, 0)),
        axis.title.y = element_text(size = axis.title.size + 2,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 2,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 2),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none")

# annotate
# Canada
f.nar.sale <- f.nar.sale + annotate("text", x = 2014.5, y = 14,
                                    label = "Canada", color = colors[[1]],
                                    size = 5)
# China
f.nar.sale <- f.nar.sale + annotate("text", x = 2014.5, y = 29,
                                    label = "China", color = colors[[2]],
                                    size = 5)
# India
f.nar.sale <- f.nar.sale + annotate("text", x = 2014.5, y = 8,
                                    label = "India", color = colors[[3]],
                                    size = 5)
# Mexico
f.nar.sale <- f.nar.sale + annotate("text", x = 2014, y = 3.5,
                                    label = "Mexico", color = colors[[4]],
                                    size = 5)
# UK
f.nar.sale <- f.nar.sale + annotate("text", x = 2015, y = 2.5,
                                    label = "UK", color = colors[[5]],
                                    size = 5)

f.nar.sale

# save
pdf(file = paste(FIG_DIR, "Figure-S1.pdf", sep = "/"), 
    width = 6, height = 6)
print(f.nar.sale)
dev.off()

# ggsave(file = paste(FIG_DIR, "Figure-S1.eps", sep = "/"), f.nar.sale,
#     width = 6, height = 6)

